package main.java.tmi.clearance.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import main.java.tmi.clearance.service.ClearanceService;
import main.java.tmi.clearance.service.ClearanceVO;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

/**
 * Test Service Implementation
 *
 * @author ssong
 * @since 2010. 09. 10.
 */
@Service
public class ClearanceServiceImpl implements ClearanceService {
	
	// Logger
	private Log logger;
	
	@Resource(name = "clearanceDAO")
	private ClearanceDAO clearanceDao;
	
	/**
	 * The Constructor
	 */
	public ClearanceServiceImpl() {
		logger = LogFactory.getLog(this.getClass());
	}
	
	//조회
	@Override
	public Map<String,String> getClearanceView(Map<String,String> param) throws Exception {

		return (Map<String,String>)clearanceDao.getClearanceView(param);

		//return resultIdx > 0 ? true : false;
	}
	
	
	//packign detail조회
	@Override
	public List<ClearanceVO> getClearanceDetailList(Map<String,String> param) throws SQLException {
		List<ClearanceVO> list = clearanceDao.getClearanceDetailList(param);
		System.out.println("====================="+list);
		return list;
	}
	
	//저장
	@Override
	public void addClearance(Map<String, String> param, HttpServletRequest request) throws Exception {
		// TODO Auto-generated method stub

	    String clearNo = clearanceDao.addClearance(param);
	    param.put("clearNo", clearNo); 

		//패킹 상세 		
		String batchNoTmp[] = request.getParameterValues("batchNo");
		String ordSeqTmp[] 	  = request.getParameterValues("ordSeq");
		String goodsCodeTmp[] 	  = request.getParameterValues("goodsCode");
		String qtyTmp[] 	  = request.getParameterValues("qty");
		
		System.out.println("===========>ordSeqTmp.length: "+ ordSeqTmp.length);
		if(ordSeqTmp.length > 0){
			Map detailParamMap = new HashMap();
			for (int i=0 ; i < ordSeqTmp.length ; i++){
				
				if ( StringUtils.isNotEmpty(ordSeqTmp[i]) ){
					detailParamMap.clear();	
					System.out.println("===========>ordSeqTmp[i]: "+ ordSeqTmp[i]);			
					detailParamMap.put("ordNo1", param.get("ordNo1"));
					detailParamMap.put("ordNo2", param.get("ordNo2"));
					detailParamMap.put("ordPseq", param.get("ordPseq"));
					detailParamMap.put("mawb", param.get("mawb"));
					detailParamMap.put("hawb", param.get("hawb"));
					detailParamMap.put("clearNo", param.get("clearNo"));
					detailParamMap.put("ordSeq", ordSeqTmp[i]);
					detailParamMap.put("batchNo", batchNoTmp[i]);
					detailParamMap.put("qty", qtyTmp[i]);
					detailParamMap.put("goodsCode", goodsCodeTmp[i]);
					detailParamMap.put("sesseionId", param.get("sesseionId") );
					clearanceDao.addClearanceDetail(detailParamMap);
				}
			}
		}
	}
	
	

	//수정
	@Override
	public boolean modifyClearance(Map<String, String> param, HttpServletRequest request) throws Exception {
		int resultCnt ;
		
		resultCnt = clearanceDao.modifyClearance(param);
			
		//패킹 상세 		
		String batchNoTmp[] = request.getParameterValues("batchNo");
		String ordSeqTmp[] 	  = request.getParameterValues("ordSeq");
		String goodsCodeTmp[] 	  = request.getParameterValues("goodsCode");
		String qtyTmp[] 	  = request.getParameterValues("qty");
		
		if(ordSeqTmp.length > 0 && qtyTmp != null){
			Map detailParamMap = new HashMap();
			for (int i=0 ; i < ordSeqTmp.length ; i++){
				
				if ( StringUtils.isNotEmpty(ordSeqTmp[i]) ){
					detailParamMap.clear();	
					System.out.println("===========>ordSeqTmp[i]: "+ ordSeqTmp[i]);			
					detailParamMap.put("ordNo1", param.get("ordNo1"));
					detailParamMap.put("ordNo2", param.get("ordNo2"));
					detailParamMap.put("ordPseq", param.get("ordPseq"));
					detailParamMap.put("mawb", param.get("mawb"));
					detailParamMap.put("hawb", param.get("hawb"));
					detailParamMap.put("clearNo", param.get("clearNo"));
					detailParamMap.put("ordSeq", ordSeqTmp[i]);
					detailParamMap.put("batchNo", batchNoTmp[i]);
					detailParamMap.put("qty", qtyTmp[i]);
					detailParamMap.put("goodsCode", goodsCodeTmp[i]);
					detailParamMap.put("sesseionId", param.get("sesseionId") );
					clearanceDao.addClearanceDetail(detailParamMap);
				}
			}
		}
		
		return resultCnt > 0 ? true : false;
	}
	
	//수정
	@Override
	public boolean clearanceDetailUpdate(Map<String, String> param) throws Exception {
		int resultCnt ;
		resultCnt = clearanceDao.clearanceDetailUpdate(param);
		return resultCnt > 0 ? true : false;
	}
	
	   //확정
    @Override
    public boolean confirmPacking(Map<String, String> param) throws Exception {
        int resultCnt ;
        resultCnt = clearanceDao.confirmPacking(param);
        return resultCnt > 0 ? true : false;
    }
    
	//삭제
	@Override
	public boolean deleteDo(Map<String, String> param) throws Exception {
		int resultCnt ;
					clearanceDao.deleteDoDep(param);
		resultCnt = clearanceDao.deleteDoHead(param);
					clearanceDao.noConfirmPacking(param);
		
		return resultCnt > 0 ? true : false;
	}
}
